'use strict';

/**
 * @ngdoc service
 * @name webApp.cookie
 * @description
 * # cookie
 * Factory in the webApp.
 */
angular.module('webApp')
  .factory('cookie', function ($localStorage) {
    $localStorage.$default({
      newsId: $localStorage.newsId,
      access_token: $localStorage.access_token,
      replayId : $localStorage.replayId,
      column_id: $localStorage.column_id,
      reportId: $localStorage.reportId
    });
    //判断是否支持localStorage
    if(window.localStorage){
      var getCookie = function(data)
      {
        for(var p in $localStorage){
          if(p == data){
            return $localStorage[p];
          }
        }
      };
      var delCookie = function(data)
      {
        for(var p in $localStorage){
          if(p == data){
            delete $localStorage[p];
          }
        }
      };
      var setCookie = function(data,value)
      {
        for(var p in $localStorage){
          if(p == data){
            $localStorage[p] = value;
          }
        }
      };
    }
    else{
      alert('不支持此类浏览器！');
    }
    return{
      //写入cookie
      setCookie :function(name,value,time){
        setCookie(name,value,time);
      },
      //删除cookie
      delCookie: function(name){
        delCookie(name);
      },
      //读取cookie
      getCookie: function(name){
        return getCookie(name);
      }
    }
  });
